//
//  CTAppInfo.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/3/19.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTAppInfo.h"
#import <Foundation/Foundation.h>
#import "CTDebugManager.h"
#import "CTFile.h"
#import "CTTextManager.h"

@implementation CTAppInfo


#pragma mark app basic info

+(NSString *)projectName
{
    NSString *executableFile = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey];
    
    return executableFile;
}
+(NSString *)projectVersion
{
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
    
    return version;
}
+(NSString *)appName
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleName"];
    
    return app_Name;
}
+(NSString *)appVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [NSString stringWithFormat:@"%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]];
    
    return app_Version;
}
+(NSString *)appBuildVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    return app_build;
}



+(NSString *)appCacheCalculate
{
    long long folderSize = 0;
    folderSize = [self folderSizeAtPathWithCaches]+[self folderSizeAtPathWithCaches_Share];
    
//   [self folderSizeAtPathWithCacheDataMessage]+
    
    NSString *str = [NSString stringWithFormat:@"%.2fM",folderSize/(1024.0*1024.0)];
    return str;
}
//计算Caches下文件缓存大小
+(long long)folderSizeAtPathWithCaches
{
    NSString * documentD = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSString * path = [documentD stringByAppendingPathComponent:@"Caches/com.hackemist.SDWebImageCache.default"];
    
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];

    }
    return  folderSize;
}
//计算Caches_SurpriseFish.PicoPico.ConstellationTalk大小
+(long long)folderSizeAtPathWithCaches_Share
{
    NSString * documentD = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSString * path = [documentD stringByAppendingPathComponent:@"Caches/SurpriseFish.PicoPico.ConstellationTalk"];
    
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
        
    }
    return  folderSize;
}
//计算cacheData下文件缓存大小
+(long long)folderSizeAtPathWithCacheDataMessage
{
    NSString * documentD = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
    NSString * path = [documentD stringByAppendingPathComponent:@"cacheData"];
    
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:path] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }

    return folderSize;
}
//单个文件的大小
+(long long)fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

+ (void)clearAppCache
{
    @try {
        NSString * documentD = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
        NSString * modelSqlite_wal = [documentD stringByAppendingPathComponent:@"Caches/SurpriseFish.PicoPico.ConstellationTalk"];
        NSFileManager *fileManager=[NSFileManager defaultManager];
        [fileManager removeItemAtPath:modelSqlite_wal error:nil];
        
//        NSString * modelSqlite_cacheData = [documentD stringByAppendingPathComponent:@"cacheData"];
//        [fileManager removeItemAtPath:modelSqlite_cacheData error:nil];
        
        NSString * modelSqlite_default = [documentD stringByAppendingPathComponent:@"Caches/com.hackemist.SDWebImageCache.default"];
        [fileManager removeItemAtPath:modelSqlite_default error:nil];
        
    }
    @catch (NSException *exception) {
        [CTDebugManager showErrorLogInfo:@"Caches删除异常，失败！！"];
    }
//    dispatch_async(
//                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
//                   , ^{
//                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
//                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
//                       NSLog(@"files :%lu",(unsigned long)[files count]);
//                       for (NSString *p in files) {
//                           NSError *error;
//                           NSString *path = [cachPath stringByAppendingPathComponent:p];
//                           if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
//                               [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
//                           }
//                       }
//                       [self performSelectorOnMainThread:@selector(clearCacheSuccess)withObject:nil waitUntilDone:YES];});


}

-(void)clearCacheSuccess
{
    NSLog(@"清理成功");
}


+(BOOL)isAppOnline
{
    BOOL isOnline;
    
    @try {
        NSString * isOnlineStr = [CTFile getFile:@"switch.text" fromDoc:CTFileDocDocuments fromPath:@"switch"];
        
        if ([[CTTextManager shareInstance] isBlankString:isOnlineStr] == YES) {
            //为空，未上线
            isOnline = NO;
        }else{
            if ([isOnlineStr isEqualToString:@"0"]) {
                //未上线
                isOnline = NO;
            }else{
                //上线
                isOnline = YES;
            }
        }
        
    }
    @catch (NSException *exception) {
        isOnline = NO;
    }
    
    return isOnline;
}


@end
